package com.java.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;

import static com.java.utils.HTTPUtils.post;

public class FaceUtils {

    private static  HashMap<String,String> map = new HashMap<>();

    static{
        try {
            Properties properties = new Properties();
            InputStream resourceAsStream = FaceUtils.class.getResourceAsStream("/application.yml");
            properties.load(resourceAsStream);
            map.put("api_key",properties.getProperty("API_Key"));
            map.put("api_secret", properties.getProperty("API_Secret"));
            map.put("display_name", properties.getProperty("DISPLAY_NAME"));
            map.put("outer_id", properties.getProperty("ORTER_ID"));
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("读取失败");
        }
    }
    /**
     * 根据传入的图片进行人脸检测功能
     * @param file   图片
     * @return
     */
    public static String detect(File file) throws Exception {
    //    file = new File("D:\\WorkSpeace\\Idea2021Yeas\\faceplusplus-demo\\src\\main\\resources\\static\\01.jpg");  //你的本地图片路径
        byte[] buff = HTTPUtils.getBytesFromFile(file);
        String url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
        HashMap<String, byte[]> byteMap = new HashMap<>();
        //   map.put("return_landmark", "1");
        //   map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
        byteMap.put("image_file", buff);
            byte[] bacd = post(url, map, byteMap);
            //符合JSON格式的字符串
            String str = new String(bacd);
            if (str.indexOf("error_message")!=-1){
                return "请求错误！";
            }
            JSONObject object = JSONObject.parseObject(str);
            Integer face_num = (Integer) object.get("face_num");
            if (face_num==1){
                JSONArray faces = (JSONArray) object.get("faces");
                JSONObject jsonObject = faces.getJSONObject(0);
                String face_token = (String)jsonObject.get("face_token");
                return  face_token;
            }
        return  null;
    }

    public static boolean CreateFaceSet() throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/create";
            byte[] bacd = post(url, map, null);
            String s = new String (bacd);
            System.out.println(s);
            if (s.indexOf("error_message")!=-1){
                 return false;
            }
            return true;
    }

    public static  boolean  addFaceSet(String face_token) throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/addface";
              map.put("face_tokens",face_token);
             byte[] bacd = post(url, map, null);
             String s = new String(bacd);
             System.out.println(s);
             if (s.indexOf("error_message")!=-1){
                 return false;
             }
             return true;
    }

    /**
     *人脸查询
     * @param face_token
     * @return
     * @throws Exception
     */
    public static  boolean SearchFace(String face_token) throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/search";
        map.put("face_token",face_token);
        byte[] bacd = post(url, map, null);
        String str = new String(bacd);
        System.out.println(str);
        if(str.indexOf("error_message")!=-1){
            return false;
        }
        JSONObject jsonObject = JSONObject.parseObject(str);
        JSONObject  thresholds = (JSONObject) jsonObject.get("thresholds");
        double le5 = thresholds.getDouble("1e-5");  //表示 误识率为十万分之一
        JSONArray results = jsonObject.getJSONArray("results");
        if (results != null && results.size()>=1){
            JSONObject object = results.getJSONObject(0);
            double confidence = object.getDouble("confidence");
            if (confidence>le5){
                return true;
            }
        }
        return false;
    }
}